<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入流程名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleDeploy"
          v-hasPermi="['workflow:bpmn:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"

          v-hasPermi="['workflow:bpmn:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['workflow:bpmn:suspend']"
        >挂起</el-button>
      </el-col>
            <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['workflow:bpmn:active']"
        >激活</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="definitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程ID" align="center" prop="id" />
      <el-table-column label="流程名称" align="center" prop="name" />
      <el-table-column label="流程Key" align="center" prop="key" />
      <el-table-column label="流程版本号" align="center" prop="version" />
      <el-table-column label="部署名称" align="center" prop="deployName" />
      <el-table-column label="部署时间" align="center" prop="deployTime" width="180">
        <template #default="scope">
          <span>{{ scope.row.deployTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">

          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 部署流程对话框 -->
    <el-dialog title="部署流程定义" v-model="deployOpen" width="500px" append-to-body>
      <el-form ref="deployRef" :model="deployForm" :rules="deployRules" label-width="100px">
        <el-form-item label="流程文件" prop="file">
          <el-upload
            ref="uploadRef"
            :auto-upload="false"
            accept=".bpmn20.xml,.xml"
            v-model:file-list="fileList"
            :limit="1"
          >
            <el-button type="primary">选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">只能上传.bpmn20.xml或.xml文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDeploy">确 定</el-button>
          <el-button @click="cancelDeploy">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WorkflowDefinition" lang="ts">
import {API_WORKFLOW } from "@/api/workflow/definition";
import type {UploadUserFile} from 'element-plus'
import { usePlugins } from '@/plugins'

const plugins = usePlugins()
const { proxy } = getCurrentInstance();

const definitionList = ref([]);
const deployOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const fileList = ref<UploadUserFile[]>([])

const data = reactive({
  deployForm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    processName: null,
  },
  deployRules: {}
});

const { queryParams, deployForm, deployRules } = toRefs(data);

/** 查询流程定义列表 */
function getList() {
  loading.value = true;
  API_WORKFLOW.listDefinition(queryParams.value).then(response => {
    definitionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.deploymentId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 部署流程按钮操作 */
function handleDeploy() {
  reset();
  deployOpen.value = true;
}

/** 文件选择处理 */
function handleFileChange(file, fileList) {
  fileList.value = fileList;
}

/** 提交部署 */
function submitDeploy() {
  if (fileList.value.length === 0) {
    plugins.$modal.msgError("请选择要部署的文件");
    return;
  }
  
  const formData = new FormData();
  const xmlFile = fileList.value[0];
  formData.append('file', xmlFile.raw!);
  //formData.append('deploymentName',xmlFile.name.split('.')[0])
  API_WORKFLOW.deployProcess(formData).then(response => {
    plugins.$modal.msgSuccess("部署成功");
    deployOpen.value = false;
    getList();
  });
}

/** 取消部署 */
function cancelDeploy() {
  deployOpen.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  deployForm.value = {};
  fileList.value = [];
  proxy.resetForm("deployRef");
}

/** 删除按钮操作 */
function handleDelete(row) {
  const deploymentIds = row.deploymentId || ids.value;
  plugins.$modal.confirm('是否确认删除流程定义编号为"' + deploymentIds + '"的数据项？').then(function() {
    return API_WORKFLOW.delDefinition(deploymentIds);
  }).then(() => {
    getList();
    plugins.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
